home *** CD-ROM | disk | FTP | other *** search
Text File | 1975-04-26 | 24.6 KB | 1,196 lines |
- head 1.8;
- branch ;
- access ;
- symbols ;
- locks ;
- comment @@;
-
-
- 1.8
- date 92.03.01.10.26.53; author melling; state Exp;
- branches ;
- next 1.7;
-
- 1.7
- date 92.03.01.05.07.36; author melling; state Exp;
- branches ;
- next 1.6;
-
- 1.6
- date 92.02.29.20.35.44; author melling; state Exp;
- branches ;
- next 1.5;
-
- 1.5
- date 92.02.28.05.18.53; author melling; state Exp;
- branches ;
- next 1.4;
-
- 1.4
- date 91.12.19.10.12.37; author melling; state Exp;
- branches ;
- next 1.3;
-
- 1.3
- date 91.12.16.17.22.03; author melling; state Exp;
- branches ;
- next 1.2;
-
- 1.2
- date 91.12.07.07.49.02; author melling; state Exp;
- branches ;
- next 1.1;
-
- 1.1
- date 91.12.07.03.36.54; author melling; state Exp;
- branches ;
- next ;
-
-
- desc
- @Tetris 1.1
- @
-
-
- 1.8
- log
- @Release 1.3
- @
- text
- @#import <appkit/Application.h>
- #import <appkit/NXImage.h>
- #import <appkit/Window.h>
- #import <dpsclient/wraps.h>
- #import "Piece.h"
- #import "TetMatrix.h"
-
- // The number of pixels to move a block at once when it is dropped.
- //static const float ANM_DELTA = 12.0;
- static const float ANM_DELTA = 28.0;
-
- extern long random();
- extern void srandom(int seed);
- extern int getpid();
-
- @@implementation Piece
-
- #define PIECE_NULL (unsigned char *)0;
- #define SHAPE_NULL (struct shape *)0;
- const int NUM_ROTATIONS = 4;
- const int NUM_SHAPES=7;
-
- #define XMIN_BOUNDS(BOUNDS) ((BOUNDS & 0xc0) >> 6)
- #define YMIN_BOUNDS(BOUNDS) ((BOUNDS & 0x30) >> 4)
- #define XMAX_BOUNDS(BOUNDS) ((BOUNDS & 0xc) >> 2)
- #define YMAX_BOUNDS(BOUNDS) (BOUNDS & 0x3)
-
- - (void) setShapes
- {
- /* ** 1st shape ** */
-
- shapes[0].table[0][0] = 0x0;
- shapes[0].table[0][1] = 0xf; // ####
- shapes[0].table[0][2] = 0x0;
- shapes[0].table[0][3] = 0x0; // x,y x1,y1
- shapes[0].bounds[0] = 0x2e; // (0, 2, 3, 2) 3*4 + 2 = 14 = 0xe
- shapes[0].points[0] = 10; // This piece is worth 10 points
-
- shapes[0].table[1][0] = 0x4; // #
- shapes[0].table[1][1] = 0x4; // #
- shapes[0].table[1][2] = 0x4; // #
- shapes[0].table[1][3] = 0x4; // #
- shapes[0].bounds[1] = 0x47; // (1, 0, 1, 3)
- shapes[0].points[1] = 16;
-
- shapes[0].table[2][0] = 0x0;
- shapes[0].table[2][1] = 0xf; // ####
- shapes[0].table[2][2] = 0x0;
- shapes[0].table[2][3] = 0x0;
- shapes[0].bounds[2] = 0x2e; // (0, 2, 3, 2)
- shapes[0].points[2] = 10;
-
- shapes[0].table[3][0] = 0x4; // #
- shapes[0].table[3][1] = 0x4; // #
- shapes[0].table[3][2] = 0x4; // #
- shapes[0].table[3][3] = 0x4; // #
- shapes[0].bounds[3] = 0x47; // (1, 0, 1, 3)
- shapes[0].points[3] = 16;
-
-
- /* ** 2nd shape ** */
-
- shapes[1].table[0][0] = 0xc; // ##
- shapes[1].table[0][1] = 0xc; // ##
- shapes[1].table[0][2] = 0x0;
- shapes[1].table[0][3] = 0x0;
- shapes[1].bounds[0] = 0x27; // (0, 2, 1, 3)
- shapes[1].points[0] = 12;
-
- shapes[1].table[1][0] = 0xc; // ##
- shapes[1].table[1][1] = 0xc; // ##
- shapes[1].table[1][2] = 0x0;
- shapes[1].table[1][3] = 0x0;
- shapes[1].bounds[1] = 0x27; // (0, 2, 1, 3)
- shapes[1].points[1] = 12;
-
- shapes[1].table[2][0] = 0xc; // ##
- shapes[1].table[2][1] = 0xc; // ##
- shapes[1].table[2][2] = 0x0;
- shapes[1].table[2][3] = 0x0;
- shapes[1].bounds[2] = 0x27; // (0, 2, 1, 3)
- shapes[1].points[2] = 12;
-
- shapes[1].table[3][0] = 0xc; // ##
- shapes[1].table[3][1] = 0xc; // ##
- shapes[1].table[3][2] = 0x0;
- shapes[1].table[3][3] = 0x0;
- shapes[1].bounds[3] = 0x27; // (0, 2, 1, 3)
- shapes[1].points[3] = 12;
-
-
- /* ** 3rd shape ** */
-
- shapes[2].table[0][0] = 0x4; // #
- shapes[2].table[0][1] = 0xe; // ###
- shapes[2].table[0][2] = 0x0;
- shapes[2].table[0][3] = 0x0;
- shapes[2].bounds[0] = 0x2b; // (0, 2, 2, 3)
- shapes[2].points[0] = 10;
-
- shapes[2].table[1][0] = 0x4; // #
- shapes[2].table[1][1] = 0x6; // ##
- shapes[2].table[1][2] = 0x4; // #
- shapes[2].table[1][3] = 0x0;
- shapes[2].bounds[1] = 0x5b; // (1, 1, 2, 3)
- shapes[2].points[1] = 10;
-
- shapes[2].table[2][0] = 0x0;
- shapes[2].table[2][1] = 0xe; // ###
- shapes[2].table[2][2] = 0x4; // #
- shapes[2].table[2][3] = 0x0;
- shapes[2].bounds[2] = 0x1a; // (0, 1, 2, 2)
- shapes[2].points[2] = 12;
-
- shapes[2].table[3][0] = 0x4; // #
- shapes[2].table[3][1] = 0xc; // ##
- shapes[2].table[3][2] = 0x4; // #
- shapes[2].table[3][3] = 0x0;
- shapes[2].bounds[3] = 0x17; // (0, 1, 1, 3)
- shapes[2].points[3] = 10;
-
-
- // 4th shape
-
- shapes[3].table[0][0] = 0xc; // ##
- shapes[3].table[0][1] = 0x6; // ##
- shapes[3].table[0][2] = 0x0;
- shapes[3].table[0][3] = 0x0;
- shapes[3].bounds[0] = 0x2b; // (0, 2, 2, 3)
- shapes[3].points[0] = 12;
-
- shapes[3].table[1][0] = 0x4; // #
- shapes[3].table[1][1] = 0xc; // ##
- shapes[3].table[1][2] = 0x8; // #
- shapes[3].table[1][3] = 0x0;
- shapes[3].bounds[1] = 0x17; // (0, 1, 1, 3)
- shapes[3].points[1] = 14;
-
- shapes[3].table[2][0] = 0xc; // ##
- shapes[3].table[2][1] = 0x6; // ##
- shapes[3].table[2][2] = 0x0;
- shapes[3].table[2][3] = 0x0;
- shapes[3].bounds[2] = 0x2b; // (0, 2, 2, 3)
- shapes[3].points[2] = 12;
-
- shapes[3].table[3][0] = 0x4; // #
- shapes[3].table[3][1] = 0xc; // ##
- shapes[3].table[3][2] = 0x8; // #
- shapes[3].table[3][3] = 0x0;
- shapes[3].bounds[3] = 0x17; // (0, 1, 1, 3)
- shapes[3].points[3] = 14;
-
-
- // 5th shape
-
- shapes[4].table[0][0] = 0x6; // ##
- shapes[4].table[0][1] = 0xc; // ##
- shapes[4].table[0][2] = 0x0;
- shapes[4].table[0][3] = 0x0;
- shapes[4].bounds[0] = 0x2b; // (0, 2, 2, 3)
- shapes[4].points[0] = 12;
-
- shapes[4].table[1][0] = 0x8; // #
- shapes[4].table[1][1] = 0xc; // ##
- shapes[4].table[1][2] = 0x4; // #
- shapes[4].table[1][3] = 0x0;
- shapes[4].bounds[1] = 0x17; // (0, 1, 1, 3)
- shapes[4].points[1] = 14;
-
- shapes[4].table[2][0] = 0x6; // ##
- shapes[4].table[2][1] = 0xc; // ##
- shapes[4].table[2][2] = 0x0;
- shapes[4].table[2][3] = 0x0;
- shapes[4].bounds[2] = 0x2b; // (0, 2, 2, 3)
- shapes[4].points[2] = 12;
-
- shapes[4].table[3][0] = 0x8; // #
- shapes[4].table[3][1] = 0xc; // ##
- shapes[4].table[3][2] = 0x4; // #
- shapes[4].table[3][3] = 0x0;
- shapes[4].bounds[3] = 0x17; // (0, 1, 1, 3)
- shapes[4].points[3] = 14;
-
-
- // 6th shape
-
- shapes[5].table[0][0] = 0x2; // #
- shapes[5].table[0][1] = 0xe; // ###
- shapes[5].table[0][2] = 0x0;
- shapes[5].table[0][3] = 0x0;
- shapes[5].bounds[0] = 0x2b; // (0, 2, 2, 3)
- shapes[5].points[0] = 6;
-
- shapes[5].table[1][0] = 0x8; // #
- shapes[5].table[1][1] = 0x8; // #
- shapes[5].table[1][2] = 0xc; // ##
- shapes[5].table[1][3] = 0x0;
- shapes[5].bounds[1] = 0x17; // (0, 1, 1, 3)
- shapes[5].points[1] = 7;
-
- shapes[5].table[2][0] = 0xe; // ###
- shapes[5].table[2][1] = 0x8; // #
- shapes[5].table[2][2] = 0x0;
- shapes[5].table[2][3] = 0x0;
- shapes[5].bounds[2] = 0x2b; // (0, 2, 2, 3)
- shapes[5].points[2] = 6;
-
- shapes[5].table[3][0] = 0xc; // ##
- shapes[5].table[3][1] = 0x4; // #
- shapes[5].table[3][2] = 0x4; // #
- shapes[5].table[3][3] = 0x0;
- shapes[5].bounds[3] = 0x17; // (0, 1, 1, 3)
- shapes[5].points[3] = 7;
-
-
- // 7th shape
-
- shapes[6].table[0][0] = 0xe; // ###
- shapes[6].table[0][1] = 0x2; // #
- shapes[6].table[0][2] = 0x0;
- shapes[6].table[0][3] = 0x0;
- shapes[6].bounds[0] = 0x2b; // (0, 2, 2, 3)
- shapes[6].points[0] = 6;
-
- shapes[6].table[1][0] = 0x4; // #
- shapes[6].table[1][1] = 0x4; // #
- shapes[6].table[1][2] = 0xc; // ##
- shapes[6].table[1][3] = 0x0;
- shapes[6].bounds[1] = 0x17; // (0, 1, 1, 3)
- shapes[6].points[1] = 7;
-
- shapes[6].table[2][0] = 0x8; // #
- shapes[6].table[2][1] = 0xe; // ###
- shapes[6].table[2][2] = 0x0;
- shapes[6].table[2][3] = 0x0;
- shapes[6].bounds[2] = 0x2b; // (0, 2, 2, 3)
- shapes[6].points[2] = 6;
-
- shapes[6].table[3][0] = 0xc; // ##
- shapes[6].table[3][1] = 0x8; // #
- shapes[6].table[3][2] = 0x8; // #
- shapes[6].table[3][3] = 0x0;
- shapes[6].bounds[3] = 0x17; // (0, 1, 1, 3)
- shapes[6].points[3] = 7;
- }
-
- BOOL resize(NXSize *aSize)
- {
- BOOL resized = NO;
-
- if (aSize->height > 32) {
- aSize->height = 32;
- resized = YES;
- }
-
- if (aSize->width > 32) {
- aSize->width = 32;
- resized = YES;
- }
- return resized;
- }
-
- - findBlockImages
- {
- int i;
- char buffer[20];
- id image;
- char *blockName;
- BOOL isGameColor = [NXApp isGameColor];
-
- if (isGameColor) {
- blockName = "ColorBlock";
- } else {
- blockName = "MonoBlock";
- }
- for (i=0; i<4; i++) {
- sprintf(buffer, "%s%d", blockName, i+1);
- image = [NXImage findImageNamed:buffer];
- if (image == NULL) {
- fprintf(stderr, "Couldn't find block named %s\n", buffer);
- exit(0);
- }
- blockImage[i] = image;
- }
- // Assuming all the blocks are the same size.
-
- [blockImage[0] getSize:&blockSize];
- if (resize(&blockSize)) {
-
- #ifdef DEBUG
- printf("resizing Block Image\n");
- #endif
- [bitmap setScalable:YES];
- [bitmap setSize:&blockSize];
- }
-
- return self;
- }
-
- - init
- {
- [super init];
-
- [self setShapes];
-
- curCol = curRow = 0;
- currentPiece = PIECE_NULL;
- currentShape = SHAPE_NULL;
- currentRotation = shapenum = 0;
-
- invalidRect.origin.x = invalidRect.origin.y =
- invalidRect.size.width = invalidRect.size.height = 0.0;
-
- // Initialized the animation Rect
- {
- NXSize tmp;
- [self findBlockImages];
-
- // Limit size of block image
- [blockImage[0] getSize:&tmp];
- resize(&tmp);
-
- tmp.height *= 4;
- tmp.width *= 4;
-
- anmShape = [[NXImage alloc] initSize:&tmp];
- [anmShape lockFocus];
- PSsetalpha(0.0); // make this block transparent
- PSrectfill(0., 0., tmp.width, tmp.height);
- PSsetalpha(1.0);
- }
- anmBackground = nil;
- viewVarsInited = NO;
- dropPoints = 0;
- srandom(getpid()); // Initialize random number generator
-
- #ifdef DEBUG
- printf("Piece object initialized\n");
- #endif
-
- return self;
- }
-
- /*
- * Draw the Piece.
- * The focus must be locked by on calling object's view before invoking this method.
- */
- - draw:sender
- {
- unsigned char mask;
- int xc, yc;
- int ycmax;
- NXPoint dPt;
-
- ycmax = MAX_SHAPE_SIZE -
- YMIN_BOUNDS(currentShape->bounds[currentRotation]);
- for (yc = MAX_SHAPE_SIZE -
- YMAX_BOUNDS(currentShape->bounds[currentRotation]) - 1;
- yc < ycmax; yc++) {
- mask = 1 << (MAX_SHAPE_SIZE - 1);
- for (xc = 0; xc < MAX_SHAPE_SIZE; xc++) {
- if (currentPiece[yc] & mask) {
- // Call the sender's parent object
- [sender point:&dPt for:curRow + MAX_SHAPE_SIZE - yc - 1 :curCol + xc];
- [bitmap composite:NX_SOVER toPoint:&dPt];
- }
- mask >>= 1;
- }
- }
- return self;
- }
-
-
- /*
- * Determine what the next piece to drop will be.
- *
- */
- - newPiece
- {
- int bitmapNum;
-
- #ifdef DEBUG
- printf("Generating a new piece\n");
- #endif
-
- shapenum = random() % NUM_SHAPES;
- // shapenum = 0;
- currentShape = &shapes[shapenum];
- currentRotation = random() % NUM_ROTATIONS;
- currentPiece = currentShape->table[currentRotation];
-
- bitmapNum = random() % NUM_BITMAPS;
-
- bitmap = blockImage[bitmapNum];
-
- if (bitmap == NULL) {
- printf("Block %d not found\n", bitmapNum);
- }
- return self;
- }
-
- /*
- * Needed so other objects can query to set the Window and View sizes.
- */
- - getBlockSize:(NXSize *) size
- {
- *size = blockSize;
- return self;
- }
-
- /*
- * So other objects can query Piece.m about images
- */
- - getBlockImage:(int) blockNum
- {
- return blockImage[blockNum];
- }
-
- - (struct pieceInfo *)pieceInfo
- {
- static struct pieceInfo info;
-
- info.bitmap = bitmap;
- info.shape = shapenum;
- info.rotation = currentRotation;
- return &info;
- }
-
-
- - setPiece:(struct pieceInfo *)info
- {
- bitmap = info->bitmap;
- [bitmap getSize:&blockSize];
- resize(&blockSize);
- shapenum = info->shape;
- currentShape = &shapes[shapenum];
- currentRotation = info->rotation;
- currentPiece = currentShape->table[currentRotation];
- return self;
- }
-
- /*
- * Get the next piece.
- *
- */
- - (BOOL)reset:sender piece:(struct pieceInfo *)info
- {
- BOOL legalMove;
- unsigned char shapeBounds;
- int xmin, xmax;
- int ymin;
- NXRect bounds;
-
- #ifdef DEBUG
- printf("reseting piece\n");
- #endif
-
- // info will contain the piece from NextMatrix if Show Next is selected
- // Otherwise, we'll generate a new piece.
-
- if (info) {
- [self setPiece:info];
- } else {
- [self newPiece];
- }
- shapeBounds = currentShape->bounds[currentRotation];
- xmin = XMIN_BOUNDS(shapeBounds);
- xmax = XMAX_BOUNDS(shapeBounds);
- ymin = YMIN_BOUNDS(shapeBounds);
- curCol = (TETRIS_COLUMNS - (xmax - xmin + 1)) / 2;
- curRow = TETRIS_ROWS - ymin - 1;
-
- if (!viewVarsInited) {
- [sender getBounds:&bounds];
- [sender getIntercell:&intercell];
-
- // Create a new animation background
- anmBackground = [[NXImage alloc] initSize:&bounds.size];
- viewVarsInited = YES;
- }
- if ((legalMove = [self legalMove:sender :curRow :curCol rotation:currentRotation])) {
- [self getInvalidRect:&invalidRect for:sender];
- [sender display:&invalidRect :1];
- }
- dropPoints = 0;
- return legalMove;
- }
-
-
- - (BOOL)legalMove:sender :(int)row :(int)column rotation:(int)rotation
- {
- unsigned char mask;
- int xc, yc;
- int ycmax;
- int ex, ey;
- unsigned char *piece;
-
- piece = currentShape->table[rotation];
- ycmax = MAX_SHAPE_SIZE - YMIN_BOUNDS(currentShape->bounds[rotation]);
- for (yc = MAX_SHAPE_SIZE -
- YMAX_BOUNDS(currentShape->bounds[rotation]) - 1; yc < ycmax; yc++) {
- mask = 1 << (MAX_SHAPE_SIZE - 1);
- for (xc = 0; xc < MAX_SHAPE_SIZE; xc++) {
- if (piece[yc] & mask) {
- ey = row + MAX_SHAPE_SIZE - yc - 1;
- ex = column + xc;
- if (ex < 0 || ex >= TETRIS_COLUMNS || ey < 0
- || [sender bitmapAt:ey :ex])
- return NO;
- }
- mask >>= 1;
- }
- }
- return YES;
- }
-
-
- - left:sender
- {
- if ([self legalMove:sender :curRow :(curCol - 1) rotation:currentRotation]) {
- curCol--;
- [sender display:&invalidRect :1];
- [self getInvalidRect:&invalidRect for:sender];
- }
- return self;
- }
-
- - right:sender
- {
- if ([self legalMove:sender :curRow :(curCol + 1) rotation:currentRotation]) {
- curCol++;
- [sender display:&invalidRect :1];
- [self getInvalidRect:&invalidRect for:sender];
- }
- return self;
- }
-
- - getInvalidRect:(NXRect *)aRect for:sender
- {
- unsigned char bounds;
- NXRect unionRect;
-
- bounds = currentShape->bounds[currentRotation];
- [sender getRect:&unionRect for:curRow + YMIN_BOUNDS(bounds) :curCol + XMIN_BOUNDS(bounds)];
- [sender getRect: aRect for:curRow + YMAX_BOUNDS(bounds) :curCol + XMAX_BOUNDS(bounds)];
- NXUnionRect(&unionRect, aRect);
-
- return self;
- }
-
- - point:(NXPoint *)thePoint for:(int)row :(int)column
- {
- unsigned char bounds = currentShape->bounds[currentRotation];
- int realx = column - curCol - XMIN_BOUNDS(bounds);
- int realy = row - curRow - YMIN_BOUNDS(bounds);
-
- thePoint->x = realx * (intercell.width + blockSize.width);
- thePoint->y = realy * (intercell.height + blockSize.height);
- return self;
- }
-
- /*
- * Called when the player his the spacebar to drop the piece.
- */
- - compositeShape:sender
- {
- if (anmShape) { // already exists
- // [self->anmShape resize:self->invalidRect.size.width :self->invalidRect.size.height];
- //self->anmShape = [NXImage newSize:&self->invalidRect.size];
- } else { // create new image
- //self->anmShape = [NXImage newSize:&self->invalidRect.size];
- //.width
- // :self->invalidRect.size.height type:NX_UNIQUEALPHABITMAP];
- // [self->anmShape setFlip:NO];
- }
- [anmShape lockFocus];
- PSsetalpha(0.0); // Fill the window with alpha
- PSrectfill(0.0, 0.0, self->invalidRect.size.width, self->invalidRect.size.height);
- PSsetalpha(1.0); // Draw image w/o alpha
- [self draw:self];
- [anmShape unlockFocus];
-
- return self;
- }
-
-
- - compositeBackground: sender
- {
- NXRect unionRect;
-
- [self getInvalidRect:&unionRect for:sender];
- NXUnionRect(&self->invalidRect, &unionRect);
- [anmBackground lockFocus];
- [sender setPieceVisible:NO];
- [sender drawSelf:&unionRect :1];
- [sender setPieceVisible:YES];
- [anmBackground unlockFocus];
- return self;
- }
-
- /*
- * Move the piece rapidly down the screen after the user hits the spacebar.
- *
- */
- - animateDrop:sender
- {
- NXRect updRect;
-
- [self getInvalidRect:&updRect for:sender];
-
- [sender lockFocus];
-
- // Erase piece by compositing the background over it.
- [anmBackground composite:NX_SOVER fromRect: &invalidRect
- toPoint:&invalidRect.origin];
-
- while (invalidRect.origin.y - ANM_DELTA > updRect.origin.y) {
- invalidRect.origin.y -= ANM_DELTA;
-
- // Show the piece dropping one increment
- [anmShape composite:NX_SOVER toPoint:&invalidRect.origin];
- [[sender window] flushWindow];
-
- // Erase the piece by showing the background
- [anmBackground composite:NX_SOVER fromRect:&invalidRect
- toPoint:&invalidRect.origin];
- }
- [sender unlockFocus];
-
- [anmShape lockFocus];
- [anmShape getSize:&updRect.size];
- PScompositerect(0.0, 0.0, updRect.size.width, updRect.size.height, NX_CLEAR);
- [anmShape unlockFocus];
- return self;
- }
-
- /*
- * Return the amount of points that we get for this piece.
- */
- - (int)points
- {
- return currentShape->points[currentRotation] + dropPoints;
- }
-
- /*
- * Called when the user hits the space bar.
- *
- */
- - drop:sender
- {
- int ymin;
- int yc;
-
- ymin = YMIN_BOUNDS(currentShape->bounds[currentRotation]);
-
- for (yc = curRow - 1; yc >= -1 - ymin; yc--) {
- if (![self legalMove:sender :yc :curCol rotation:currentRotation])
- break;
- }
- dropPoints += curRow - yc + 1; // You get extra points for dropping it
- curRow = yc + 1;
-
- [self compositeShape: sender];
- [self compositeBackground: sender];
- [self animateDrop: sender];
-
- [sender display:&invalidRect : 1];
- [self getInvalidRect:&invalidRect for:sender];
- return self;
- }
-
- /*
- * Move the piece down.
- */
- - (BOOL)down:sender
- {
- BOOL canGoDown;
-
- if ((canGoDown = [self legalMove:sender :curRow-1 :curCol rotation:currentRotation])){
- curRow--;
- [sender display:&invalidRect :1];
- [self getInvalidRect:&invalidRect for:sender];
- }
- return canGoDown;
- }
-
- - turn:sender
- {
- int newRotation = (currentRotation + 3) % NUM_ROTATIONS;
-
- if ([self legalMove:sender :curRow :curCol rotation:newRotation]) {
- currentRotation = newRotation;
- currentPiece = currentShape->table[newRotation];
- [sender display:&invalidRect :1];
- [self getInvalidRect:&invalidRect for:sender];
- }
- return self;
- }
-
- /*
- * Called when the piece has reached the bottom and can no longer be moved.
- *
- * For each block that composes the image, put the block's image id in the
- * proper iconMatrix[row][column] position. Thus to redraw the screen,
- * we need only to composite the images from the iconMatrix.
- */
- - stick:sender
- {
- unsigned char mask;
- int col, theRow;
- int rowMax;
-
- rowMax = MAX_SHAPE_SIZE - YMIN_BOUNDS(currentShape->bounds[currentRotation]);
- for (theRow = MAX_SHAPE_SIZE -
- YMAX_BOUNDS(currentShape->bounds[currentRotation]) - 1;
- theRow < rowMax; theRow++) {
- mask = 1 << (MAX_SHAPE_SIZE - 1);
- for (col = 0; col < MAX_SHAPE_SIZE; col++) {
- // If there's a block in
- if (currentPiece[theRow] & mask)
- // Set the (row, column) of the matrix equal to Block's image.
- [sender setBitmap:bitmap at:curRow + MAX_SHAPE_SIZE - theRow - 1
- :curCol + col];
- mask >>= 1;
- }
- }
- return self;
- }
-
- - (int)getCurRow
- {
- return curRow;
- }
-
- - free
- {
- if (anmShape) [anmShape free];
- if (anmBackground) [anmBackground free];
- return [super free];
- }
-
- @@end
- @
-
-
- 1.7
- log
- @*** empty log message ***
- @
- text
- @d387 1
- a387 1
-
- @
-
-
- 1.6
- log
- @*** empty log message ***
- @
- text
- @d269 1
- a269 1
- const NXScreen *deepestScreen=[NXApp colorScreen];
- d271 1
- a271 1
- if ( deepestScreen->depth != NX_TwoBitGrayDepth ) {
- d338 1
- a338 1
- printf("+++Piece object initialized\n");
- d411 8
- d442 4
- d458 2
- a459 4
- if (info)
- [self setPiece:info];
- else
- [self newPiece];
- d461 5
- d638 1
- a638 1
- * Return the points that we get for this piece.
- @
-
-
- 1.5
- log
- @*** empty log message ***
- @
- text
- @d285 12
- a310 1
- bitmapSize.width = bitmapSize.height = 0.0;
- d338 1
- a338 1
- printf("Piece object initialized\n");
- d386 1
- a386 2
- shapenum = random() % NUM_SHAPES;
- // shapenum = 1;
- a398 8
- [bitmap getSize:&bitmapSize];
- if (resize(&bitmapSize)) {
- #ifdef DEBUG
- printf("resizing Block Image\n");
- #endif
- [bitmap setScalable:YES];
- [bitmap setSize:&bitmapSize];
- }
- d402 8
- d425 2
- a426 2
- [bitmap getSize:&bitmapSize];
- resize(&bitmapSize);
- d542 2
- a543 2
- thePoint->x = realx * (intercell.width + bitmapSize.width);
- thePoint->y = realy * (intercell.height + bitmapSize.height);
- @
-
-
- 1.4
- log
- @*** empty log message ***
- @
- text
- @d5 1
- a6 1
- #import "Piece.h"
- d263 25
- a294 1
- bitmap = nil;
- d306 1
- d308 4
- a311 3
- bitmap = [NXImage findImageNamed:"Block1"];
- [bitmap getSize:&tmp];
- resize(&tmp); // Limit size of block image
- d314 1
- d369 1
- a369 1
- char *bitmapName;
- d382 1
- a382 13
- switch (random() % NUM_BITMAPS) {
- case 0:
- bitmapName = "Block1";
- break;
- case 1:
- bitmapName = "Block2";
- break;
- case 2:
- bitmapName = "Block3";
- break;
- default:
- bitmapName = "Block4";
- }
- d384 2
- a385 4
- #ifdef DEBUG
- printf("Using block image %s\n", bitmapName);
- #endif
- bitmap = [NXImage findImageNamed:bitmapName];
- d387 1
- a387 1
- printf("Block %s not found\n", bitmapName);
- @
-
-
- 1.3
- log
- @*** empty log message ***
- @
- text
- @a6 1
- #import "Cheater.h"
- d9 2
- a10 1
- static const float ANM_DELTA = 12.0;
- d247 16
- d274 1
- a274 1
- cheating = NO;
- d281 7
- a287 1
- NXSize tmp = {75., 75.}; // Must be at least 4 times block width.
- a296 1
-
- d373 3
- d377 7
- a386 4
- - (void) setCheating:(BOOL) value
- {
- cheating = value;
- }
- d403 1
- @
-
-
- 1.2
- log
- @Tetris 1.2
- @
- text
- @d1 1
- a1 1
-
- d7 1
- d9 2
- a10 1
- #define ANM_DELTA 12.0
- d20 2
- a21 2
- #define NUM_ROTATIONS 4
- #define NUM_SHAPES 7
- d28 1
- a28 1
- static void setShapes(struct shape *shapes)
- d37 1
- a37 1
- shapes[0].points[0] = 10;
- d250 4
- a253 2
- setShapes(shapes);
- x = y = 0;
- d258 1
- d277 5
- d285 4
- d304 3
- a306 2
- [sender point:&dPt for:y + MAX_SHAPE_SIZE - yc - 1 :x + xc];
- [bitmap composite:NX_COPY toPoint:&dPt];
- d314 1
- d327 3
- a329 1
- shapenum = random() % NUM_SHAPES;
- d333 1
- a333 1
-
- d347 4
- d356 5
- d391 4
- d404 2
- a405 2
- x = (TETRIS_COLUMNS - (xmax - xmin + 1)) / 2;
- y = TETRIS_ROWS - ymin - 1;
- d412 1
- a412 1
- anmBackground = [NXImage newSize:&bounds.size];
- d415 1
- a415 1
- if ((legalMove = [self legalMove:sender :y :x rotation:currentRotation])) {
- d454 2
- a455 2
- if ([self legalMove:sender :y :(x - 1) rotation:currentRotation]) {
- x--;
- d464 2
- a465 2
- if ([self legalMove:sender :y :(x + 1) rotation:currentRotation]) {
- x++;
- d475 1
- a475 1
- NXRect uRect;
- d478 3
- a480 3
- [sender getRect:&uRect for:y + YMIN_BOUNDS(bounds) :x + XMIN_BOUNDS(bounds)];
- [sender getRect: aRect for:y + YMAX_BOUNDS(bounds) :x + XMAX_BOUNDS(bounds)];
- NXUnionRect(&uRect, aRect);
- d488 2
- a489 2
- int realx = column - x - XMIN_BOUNDS(bounds);
- int realy = row - y - YMIN_BOUNDS(bounds);
- d523 1
- a523 1
- NXRect uRect;
- d525 2
- a526 2
- [self getInvalidRect:&uRect for:sender];
- NXUnionRect(&self->invalidRect, &uRect);
- d529 1
- a529 1
- [sender drawSelf:&uRect :1];
- d535 4
- a538 1
-
- d546 3
- a548 2
- // Erase piece
- [anmBackground composite:NX_COPY fromRect: &invalidRect
- d559 2
- a560 2
- [anmBackground composite:NX_COPY fromRect:&self->invalidRect
- toPoint:&self->invalidRect.origin];
- d571 3
- d590 2
- a591 2
- for (yc = y - 1; yc >= -1 - ymin; yc--) {
- if (![self legalMove:sender :yc :x rotation:currentRotation])
- d594 2
- a595 2
- dropPoints += y - yc + 1;
- y = yc + 1;
- d606 3
- a608 1
-
- d613 2
- a614 2
- if ((canGoDown = [self legalMove:sender :y-1 :x rotation:currentRotation])){
- y--;
- d625 1
- a625 1
- if ([self legalMove:sender :y :x rotation:newRotation]) {
- d635 1
- a635 1
- * The piece has reached the bottom and can no longer be moved.
- d637 3
- d644 2
- a645 2
- int xc, yc;
- int ycmax;
- d647 2
- a648 2
- ycmax = MAX_SHAPE_SIZE - YMIN_BOUNDS(currentShape->bounds[currentRotation]);
- for (yc = MAX_SHAPE_SIZE -
- d650 1
- a650 1
- yc < ycmax; yc++) {
- d652 6
- a657 3
- for (xc = 0; xc < MAX_SHAPE_SIZE; xc++) {
- if (currentPiece[yc] & mask)
- [sender setBitmap:bitmap at:y + MAX_SHAPE_SIZE - yc - 1 :x + xc];
- d664 1
- a664 1
- - (int)y
- d666 1
- a666 1
- return y;
- @
-
-
- 1.1
- log
- @Initial revision
- @
- text
- @d28 1
- a28 1
- // 1st shape
- d59 1
- a59 1
- // 2nd shape
- d90 1
- a90 1
- // 3rd shape
- d299 4
- d307 4
- d368 1
- d447 2
- a448 4
- [sender getRect:&uRect for:y + YMIN_BOUNDS(bounds)
- :x + XMIN_BOUNDS(bounds)];
- [sender getRect:aRect for:y + YMAX_BOUNDS(bounds)
- :x + XMAX_BOUNDS(bounds)];
- d468 1
- a468 1
- static void compositeShape(id self, id sender)
- d470 1
- a470 1
- if (self->anmShape) { // already exists
- d479 1
- a479 1
- [self->anmShape lockFocus];
- d484 3
- a486 1
- [self->anmShape unlockFocus];
- d489 2
- a490 1
- static void compositeBackground(id self, id sender)
- d493 1
- a493 1
-
- d496 1
- a496 1
- [self->anmBackground lockFocus];
- d500 2
- a501 1
- [self->anmBackground unlockFocus];
- d504 2
- a505 1
- static void animateDrop(id self, id sender)
- d510 1
- d513 2
- a514 2
- [self->anmBackground composite:NX_COPY fromRect: &self->invalidRect
- toPoint:&self->invalidRect.origin];
- d516 3
- a518 2
- while (self->invalidRect.origin.y - ANM_DELTA > updRect.origin.y) {
- self->invalidRect.origin.y -= ANM_DELTA;
- d520 1
- a520 1
- [self->anmShape composite:NX_SOVER toPoint:&self->invalidRect.origin];
- d522 1
- d524 1
- a524 1
- [self->anmBackground composite:NX_COPY fromRect:&self->invalidRect
- d529 5
- a533 5
- [self->anmShape lockFocus];
- [self->anmShape getSize:&updRect.size];
- PScompositerect(0.0, 0.0, updRect.size.width, updRect.size.height,
- NX_CLEAR);
- [self->anmShape unlockFocus];
- d549 1
- a549 1
-
- d551 5
- a555 4
-
- for (yc = y - 1; yc >= -1 - ymin; yc--)
- if (![self legalMove:sender :yc :x rotation:currentRotation])
- break;
- d559 3
- a561 3
- compositeShape(self, sender);
- compositeBackground(self, sender);
- animateDrop(self, sender);
- d568 1
- d572 1
- a572 1
-
- d594 4
- a602 1
- NXPoint dPt;
- @
-